#include "edit.h"
#include "ui_edit.h"
#include "widget.h"
#include "network.h"
#include "MessageType.h"
#include "tcpsocketsingleton.h"
#include <QDateTime>
#include <QTime>
#include <QVBoxLayout>

edit::edit(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::edit)
{
    ui->setupUi(this);
    this->setWindowTitle("医生端");
    this->setWindowIcon(QIcon(":doctorimg/img/doctorIcon.png"));
    // 设置主窗口背景及控件样式
    this->setStyleSheet(
        "QWidget { background-color: rgba(255, 240, 245, 0.95); }" // 整体背景为浅粉色
        "QLabel { color: #880E4F; font-size: 24px; font-weight: bold; }" // 增大字体为24px
        "QLineEdit { min-height: 40px; background-color: rgba(255, 255, 255, 0.9); "
        "border: 2px solid #F48FB1; padding: 12px; border-radius: 10px; font-size: 20px; color: black; }" // 调整输入框
        "QPushButton { min-width: 100px; min-height: 40px; background-color: #F8BBD0; color: white; border: 2px solid #F48FB1; "
        "padding: 15px 30px; border-radius: 12px; font-size: 22px; font-weight: bold; }" // 按钮背景、字体及大小设置
        "QPushButton:hover { background-color: #F48FB1; }" // 悬停时按钮背景变深粉色
    );

    // 主布局设置
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setAlignment(Qt::AlignCenter); // 垂直布局居中
    mainLayout->setSpacing(20); // 控件间距

    // 顶部标题部分
    QWidget *titleWidget = new QWidget(this);
    QGridLayout *titleLayout = new QGridLayout(titleWidget);
    titleLayout->addWidget(ui->label_4, 0, 0, Qt::AlignCenter); // 添加标题标签
    titleLayout->addWidget(ui->pushButton, 0, 1, Qt::AlignRight); // 添加退出按钮
    titleLayout->setSpacing(20);
    mainLayout->addWidget(titleWidget);

    // 医生和患者信息布局
    QGridLayout *infoLayout = new QGridLayout();
    infoLayout->setSpacing(15); // 控件间距

    // 添加医生姓名、医生ID、患者姓名、患者ID标签及输入框
    infoLayout->addWidget(ui->label_3, 0, 0); // 医生姓名标签
    infoLayout->addWidget(ui->doctorName, 0, 1); // 医生姓名输入框
    infoLayout->addWidget(ui->label, 1, 0); // 医生ID标签
    infoLayout->addWidget(ui->doctorID, 1, 1); // 医生ID输入框
    infoLayout->addWidget(ui->label_5, 2, 0); // 患者姓名标签
    infoLayout->addWidget(ui->patientName, 2, 1); // 患者姓名输入框
    infoLayout->addWidget(ui->label_6, 3, 0); // 患者ID标签
    infoLayout->addWidget(ui->patientID, 3, 1); // 患者ID输入框

    mainLayout->addLayout(infoLayout); // 将信息布局添加到主布局

    // 处方内容部分
    QHBoxLayout *prescriptionLayout = new QHBoxLayout();
    prescriptionLayout->addWidget(ui->label_2); // 添加处方标签
    prescriptionLayout->addWidget(ui->prescriptionTextEdit); // 添加处方文本框
    mainLayout->addLayout(prescriptionLayout); // 将处方内容布局添加到主布局

    // 保存按钮
    mainLayout->addWidget(ui->savePrescriptionButton, 0, Qt::AlignRight); // 添加保存处方按钮

    // 设置主布局
    this->setLayout(mainLayout);

    // if(!tcpSocket) {
    //     tcpSocket  = new QTcpSocket;
    //     // tcpSocket->connectToHost("127.0.0.1", 12345);
    //     if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
    //         tcpSocket->connectToHost("127.0.0.1", 12345);
    //     }
    // }
    tcpSocket = TcpSocketSingleton::instance().socket();

}

edit::~edit()
{
    delete ui;
}

void edit::on_savePrescriptionButton_clicked()
{
    savePrescription(); // 调用保存处方函数
}

void edit::savePrescription()
{
    QString doctorID = ui->doctorID->text();
    QString doctorName = ui->doctorName->text();
    QString patientName = ui->patientName->text();
    QString patientID = ui->patientID->text();
    QString prescriptionDetails = ui->prescriptionTextEdit->toPlainText();

    // 获取系统当前时间
    QDateTime currentTime = QDateTime::currentDateTime();
    QDate mydata = currentTime.date();
    int year = mydata.year();
    int month = mydata.month();
    int day = mydata.day();
    QTime time = currentTime.time();
    int hour = time.hour();
    int min = time.minute();
    int sec = time.second();
    QString mytime = QString("%1-%2-%3 %4:%5:%6")
                     .arg(year, 2, 10, QChar('0'))
                     .arg(month, 2, 10, QChar('0'))
                     .arg(day, 2, 10, QChar('0'))
                     .arg(hour, 2, 10, QChar('0'))
                     .arg(min, 2, 10, QChar('0'))
                     .arg(sec, 2, 10, QChar('0'));


    // 准备保存处方数据请求
    QMap<QString, QString> data;
    data.insert("type", PRESCRIPTIONDATABASE);
    data.insert("databaseoperator", QString::number(INSERT));
    data.insert("doctorname", doctorName);
    data.insert("doctorid", doctorID);
    data.insert("patientid", patientID);
    data.insert("patientname", patientName);
    data.insert("prescr", prescriptionDetails);
    data.insert("date", mytime);

    // 转换为 QByteArray 并发送
    QByteArray sendData = mapToByteArrayCustom(data);
    tcpSocket->write(sendData);
    QMessageBox::information(this, "处方", "保存处方成功");
}

void edit::on_pushButton_clicked()
{
    this->close();
    // Widget *widget = new Widget();
    // widget->show();
}
